package com.elitesland.org.rest;

import com.elitesland.core.base.ApiResult;
import com.elitesland.org.service.OrgAddressBookService;
import com.elitesland.org.vo.OrgAddressBookVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/org/abs")
@Api(value = "组织结构", tags = {"地址簿管理"})
public class OrgAddressBookController {

    private OrgAddressBookService orgAddressBookService;

    @Autowired
    public void setOrgAddressBookService(OrgAddressBookService orgAddressBookService) {
        this.orgAddressBookService = orgAddressBookService;
    }

    @GetMapping("/{type}/byrec/{id}")
    @ApiOperation("根据记录ID，和地址簿类型，返回地址簿列表")
    public ApiResult<?> listByRecId(
            @PathVariable String type,
            @PathVariable Long id
    ) {
        val ret = orgAddressBookService.listById(id, type);

        return ApiResult.ok(ret);
    }

    @PostMapping("/bank")
    @ApiOperation("批量创建银行账号信息")
    public ApiResult<?> createBankInBatch(
            @RequestBody List<OrgAddressBookVO> abs
    ) {
        orgAddressBookService.createBankInfoInBatch(abs);
        return ApiResult.ok();
    }

    @PostMapping
    @ApiOperation("批量创建地址簿信息")
    public ApiResult<?> createInBatch(
            @RequestBody List<OrgAddressBookVO> abs
    ) {
        orgAddressBookService.createInBatch(abs);
        return ApiResult.ok();
    }

    @PutMapping
    @ApiOperation("批量修改地址簿信息")
    public ApiResult<?> updateInBatch(
            @RequestBody List<OrgAddressBookVO> abs
    ) {
        orgAddressBookService.updateInBatch(abs);
        return ApiResult.ok();
    }

    @DeleteMapping
    @ApiOperation("根据地址簿记录ID，批量删除地址簿")
    public ApiResult<?> removeInBatch(
            @RequestBody List<Long> ids
    ) {
        orgAddressBookService.removeInBatch(ids);
        return ApiResult.ok();
    }

    @PutMapping("/primary/{id}")
    @ApiOperation("根据记录ID，将对应地址簿设为对应类型下的主记录")
    public ApiResult<?> setPrimary(@PathVariable Long id) {
        orgAddressBookService.setPrimary(id);
        return ApiResult.ok();
    }
}
